package com.neutech.maplestory.client.client;

import com.neutech.maplestory.client.entity.Arrow;
import com.neutech.maplestory.client.entity.Hero;
import com.neutech.maplestory.client.entity.Mob;
import com.neutech.maplestory.client.util.MapleStoryFrame;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * CLASS:MapleStoryClint：
 * TODU:游戏的主入口文件
 * date:2021/1/27、16:57
 * version:0.1
 */
public class MapleStoryClint extends MapleStoryFrame {
    public Hero hero = new Hero(this,300,410);
//    解决线程非安全问题
    public List<Arrow>arrows =new CopyOnWriteArrayList<>();
    public List<Mob>mobs = new CopyOnWriteArrayList<>();
    {
        for (int i = 0; i < 5; i++) {
            Mob mob = new Mob(this,500+i*80,429,5);
            mobs.add(mob);
        }
    }

    //public Arrow arrow = new Arrow(this,40,50,null);

    @Override
    public void init() {
        super.init();
        //添加键盘监听器
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                hero.keyPressed(e);
            }

            @Override
            public void keyReleased(KeyEvent e) {
                hero.keyReleased(e);
            }
        });
    }


    @Override
    public void paint(Graphics g) {
        hero.draw(g);
       //mobs.draw(g);
        //arrow.draw(g);
        Iterator<Arrow>itArrow = arrows.iterator();
        while (itArrow.hasNext()){
            Arrow arrow = itArrow.next();
            arrow.draw(g);
            arrow.hit(mobs);
        }
        /*for (int i = 0; i < arrows.size(); i++) {
            Arrow arrow = arrows.get(i);
            arrow.draw(g);
            arrow.hit(mobs);
            //arrows.get(i).draw(g);

        }*/
        Iterator<Mob>itMobs = mobs.iterator();
        while (itMobs.hasNext()){
            Mob mob = itMobs.next();
            mob.draw(g);
            //arrow.hit(mobs);
        }
        for (int i = 0; i < mobs.size(); i++) {
            mobs.get(i).draw(g);
        }
        Font f =g.getFont();
        g.setFont(new Font("weir雅黑",Font.BOLD,30));
        g.drawString("人物方向"+hero.dir,100,100);
        g.drawString("人物动作"+hero.action,100,150);
        g.drawString("人物动作"+hero.done,100,200);
        g.drawString("count:"+arrows.size(),100,250);
        g.drawString("count:"+mobs.size(),100,300);
        g.setFont(f);
    }

    public static void main(String[] args) {
        new MapleStoryClint().init();
    }
}
